package org.dromara.admin.utils;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpRequest;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.util.*;

public class SignApiClient {

    private static final String BASE_URL = "https://hd-cloud.test.nbhdian.com";
    private static final String APP_ID = "d75j5fx8f1yrza4o6bdxf4fozw5ghwc8";
    private static final String APP_KEY = "5y9xvgfz735c1p75vt0cu0jmsa63l2wic49b09s0x2xzcyjm29oa5ngqlehnizzv";
    private static final ObjectMapper MAPPER = new ObjectMapper();

    /**
     * data 为 Map 的接口调用
     */
    public static JsonNode post(String apiPath, Map<String, Object> dataMap) throws Exception {
        // 时间戳（秒级）
        String time = String.valueOf(System.currentTimeMillis() / 1000);
        // 随机字符串
        String nostr = UUID.randomUUID().toString().replace("-", "");

        // 升序排序
        TreeMap<String, Object> sortedData = new TreeMap<>(dataMap);
        String jsonData = MAPPER.writeValueAsString(sortedData);

        // 生成 sign
        String signStr = APP_ID + "#" + jsonData + "#" + time + "#" + APP_KEY + "#" + nostr;
        String sign = SecureUtil.md5(signStr);

        // 构造请求体
        ObjectNode root = MAPPER.createObjectNode();
        root.put("time", time);
        root.put("nostr", nostr);
        root.put("appid", APP_ID);
        root.put("sign", sign);
        root.set("data", MAPPER.valueToTree(sortedData));

        System.out.println(apiPath + "请求体: " + MAPPER.writeValueAsString(root));

        String response = HttpRequest.post(BASE_URL + apiPath)
            .header("Content-Type", "application/json")
            .body(root.toString())
            .execute()
            .body();
        System.out.println(apiPath + "响应结果: " + MAPPER.readTree(response));

        return MAPPER.readTree(response);
    }

    /**
     * data 为 List<Map> 的接口调用（如 addSigner）
     */
    public static JsonNode post(String apiPath, List<Map<String, Object>> dataList) throws Exception {
        String time = String.valueOf(System.currentTimeMillis() / 1000);
        String nostr = UUID.randomUUID().toString().replace("-", "");

        String jsonData = MAPPER.writeValueAsString(dataList);

        String signStr = APP_ID + "#" + jsonData + "#" + time + "#" + APP_KEY + "#" + nostr;
        String sign = SecureUtil.md5(signStr);

        ObjectNode root = MAPPER.createObjectNode();
        root.put("time", time);
        root.put("nostr", nostr);
        root.put("appid", APP_ID);
        root.put("sign", sign);
        root.set("data", MAPPER.valueToTree(dataList));
        System.out.println(apiPath + "请求体: " + MAPPER.writeValueAsString(root));

        String response = HttpRequest.post(BASE_URL + apiPath)
            .header("Content-Type", "application/json")
            .body(root.toString())
            .execute()
            .body();
        System.out.println(apiPath + "响应结果: " + MAPPER.readTree(response));

        return MAPPER.readTree(response);
    }
}

